package dmsxl.shuzu;

import java.util.Arrays;
import java.util.HashMap;

/**
 * Author: Zhang Dongwei
 * Date: 2023/4/6 19:44
 */
public class array5_r1_904 {

    public static void main(String[] args) {
        int[] fruits = {3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4};
        System.out.println(totalFruit(fruits));
    }

    public static int totalFruit(int[] fruits) {
        HashMap<Integer, Integer> cnt = new HashMap<>();
        int left = 0, res = 0;
        for (int right=0; right<fruits.length; right++){
            cnt.put(fruits[right], cnt.getOrDefault(fruits[right], 0)+1);
            while (cnt.size()>2){
                cnt.put(fruits[left], cnt.get(fruits[left])-1);
                if (cnt.get(fruits[left])==0){
                    cnt.remove(fruits[left]);
                }
                ++left;
            }
            res = Math.max(res, right-left+1);
        }
        return res;
    }
}
